from blog.models import Post
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from blog import config

def detail(request, category_id):
    post_list = Post.get_by_user(request.user).filter(category_id = category_id)
    paginator = Paginator(post_list, config.MAX_ITEMS_PER_PAGE)
    p = request.GET.get('page', 1)

    try:
        posts = paginator.page(p)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

    return render_to_response(
        'blog/category/detail.html',
        {'posts': posts},
        RequestContext(request)
    )
